function [ ] = plotFitHistEmissions( Eit, apcd_max, filename, t, titleString, ...
    inlogs, optfig, Elim, apcd_flag )
% Plot total costs under the emissions market and under the command and
%   control scenario
%
%   INPUTS:
%     Eit - Plant i emissions in period t
%     filestub - Root of filename in which to print figure
%     t - Emissions period
%     title - Title of figure
%     optfig - Figure options
%     Elim - Emissions axis limits
%     apcd_flag - whether or not to break up histogram into apcd_max tech

%% Data
if inlogs
    Eit = log(Eit);
end

%% Plot
% Lines
f0 = figure;
if apcd_flag == true
    hist_range = linspace(0,15,31);
    h1 = histogram(Eit(apcd_max==1),hist_range).Values';
    h2 = histogram(Eit(apcd_max==2),hist_range).Values';
    h3 = histogram(Eit(apcd_max==3),hist_range).Values';
    h4 = histogram(Eit(apcd_max==4),hist_range).Values';
    h5 = histogram(Eit(apcd_max==-1),hist_range).Values';
    h6 = histogram(Eit(apcd_max==0),hist_range).Values';
    bar(hist_range(1:end-1)+.25, [h1, h2, h3, h4, h5, h6], 'stacked','BarWidth', 1); hold on
    lgd = legend('Tech: Cyclone', 'Tech: BF', 'Tech: Scrubber', 'Tech: Esp', 'Min Observation', 'Max Possible');
    lgd.Title.String = "$E_i^{min}$ set by:";
    set(lgd, 'Interpreter','latex')
else
histogram( Eit, ...
    'Normalization', 'pdf', ...
    'facecolor', 'b', ...
    'binwidth', 0.5); hold on;
end

% Add reference lines to compare command {Emissions,Cost} to:
%   * Same emissions, cost under market
%   * Same cost, emissions under market


% Figure formatting
set(gcf, 'Color'       , 'w', ...
    'position',[0 0 500 500]);
set(gca, optfig.labProp{:}, ...
         'Box'         , 'off'             , ...
         'LineWidth'   , optfig.axisweight);

% Axis labels
if inlogs
    xlabelString = 'Log emissions (kg)';
else
    xlabelString = 'Emissions (kg)';
end
xlabel(xlabelString,optfig.textProp{:});
ylabel('Density',optfig.textProp{:});

% Title
title(titleString,optfig.textProp{:},'Interpreter','latex');

% Legend
% legend([s1, s3, s2] ,{'Eqb. bids','Shaded bids','Costs'},...
%     'Location','southoutside',...
%     'Orientation','vertical',...
%      optfig.legProp{:});
% legend boxoff;

% Axis limits must include zero
if nargin > 6
    xlim(Elim);
end

% yl = ylim();
% xl = xlim();
% ylim([0 yl(2)]);
% xlim([0 xl(2)]);

hold off;

%% Export figure to file

clear figure_property;
figure_property.units = 'inches';
figure_property.format = 'pdf';
figure_property.Width= '7.5'; % Figure width on canvas
figure_property.Height= '5'; % Figure height on canvas
figure_property.Units= 'inches';
figure_property.Renderer= 'painters';
figure_property.Resolution= '600';
chosen_figure=gcf;
set(chosen_figure,'PaperUnits','inches');
%set(chosen_figure,'PaperPositionMode','auto');
set(chosen_figure,'PaperSize',[str2num(figure_property.Width) str2num(figure_property.Height)]); % Canvas Size
set(chosen_figure,'Units','inches');

% Save all figures
%[filepath, ~, ~] = fileparts(filename);
%if not(isfolder(filepath))
%    mkdir(filepath);
%end
%fprintf(1,'Writing %s to file ...\n',filename);
% hgexport(gcf,filename,figure_property);

% Save figure for specific model type and subtype, data type, and period
% Determine model type
if contains(filename, "Iso")
    type = "Iso";
    if contains(filename, "FullNoHet")
        subtype = "FullNoHet";
    else 
        subtype = "FullHet";
    end
else
    type = "Step";
    if contains(filename, "NoFreeCyc")
        subtype = "NoFreeCyc";
    else 
        subtype = "FreeCyc";
    end
end

% Determine if predicted or observed data
predicted = contains(filename, "simul");

% Get path
[~, old_file, old_ext] = fileparts(filename);
new_filepath = erase(filename, strcat("model/", type, "/", subtype, "/", old_file, old_ext));

% Set file name
if type == "Step" && subtype == "FreeCyc" && t == 4 && predicted
    filename = strcat(new_filepath, "Figure_E3_A");
elseif type == "Step" && subtype == "FreeCyc" && t == 8 && predicted
    filename = strcat(new_filepath, "Figure_E3_B");
elseif type == "Iso" && subtype == "FullNoHet" && t == 4 && predicted
    filename = strcat(new_filepath, "Figure_E3_C");
elseif type == "Iso" && subtype == "FullNoHet" && t == 8 && predicted
    filename = strcat(new_filepath, "Figure_E3_D");
elseif type == "Iso" && subtype == "FullNoHet" && t == 4 && ~predicted
    filename = strcat(new_filepath, "Figure_E3_E");
elseif type == "Iso" && subtype == "FullNoHet" && t == 8 && ~predicted
    filename = strcat(new_filepath, "Figure_E3_F");
else
    filename = "NULL";
end    


if filename ~= "NULL"
    fprintf(1,'Writing %s to file ...\n',filename);
    hgexport(gcf,filename,figure_property);
end


end